package com.mcxx.modules.temporary.fundpay.service.read;

import com.google.common.collect.Lists;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayFamilyEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayMemberEntity;
import com.mcxx.modules.temporary.fundpay.dao.read.TmpFundPayMemberReadDao;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayFamilyEntity;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayMemberEntity;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@TaTransactional(readOnly = true)
public class TmpFundPayMemberReadService {

    @Autowired
    private TmpFundPayMemberReadDao tmpFundPayMemberReadDao;

    
    public List<TmpFundPayMemberEntity> listAll() {
    	return tmpFundPayMemberReadDao.listAll();
    }


    
    public TmpFundPayMemberEntity getById(String id) {
    	return tmpFundPayMemberReadDao.getById(id);
    }


    
    public List<TmpFundPayMemberEntity> getCapitalMemberList(List<TmpFundPayFamilyEntity> fundPayFamilyList){
        List<String> orderIdList = fundPayFamilyList.stream().map(
                familyEntity -> familyEntity.getOrderId()).collect(Collectors.toList());
        List<String> familyIdList = fundPayFamilyList.stream().map(
                familyEntity -> familyEntity.getFamilyId()).collect(Collectors.toList());
        List<List<String>> subList = Lists.partition(orderIdList,512);
        List<List<String>> subList1 = Lists.partition(familyIdList,512);

        List<TmpFundPayMemberEntity> results = new ArrayList<>();
        for (int i = 0; i < subList.size(); i++) {
            List<TmpFundPayMemberEntity> fundPayMemberEntityList = tmpFundPayMemberReadDao.getMemberCapital(subList.get(i), subList1.get(i));
            results.addAll(fundPayMemberEntityList);
        }
        if(results.size() <= 0){
            throw new BizException("没有可以添加到名册中的家庭");
        }
        return results;
    }

}
